package my.mark.mybaibaoxiang.threadPoolCheck;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author wxming.
 * @Description TODO.一个检查线程是否激活的类
 * @date 2020-6-8.
 */
@Component
public class ThearActiveCheckService {
    private static final Logger logger = LoggerFactory.getLogger(ThearActiveCheckService.class);
    private long lastRuntime = 0L;

    @Autowired
    OneThreadPoolService oneThreadPoolService;

    //@RunInAllServers
    @Scheduled(fixedRate = 3600000)
    public void run(){
        if(lastRuntime==0L){
            lastRuntime = System.currentTimeMillis();
            logger.info("检查线程是否激活首次不处理");
        }else{
            logger.info("检查开始");

            if(!oneThreadPoolService.iSSendAlive()){
                Thread sendThread = new Thread(new SendThread());
                sendThread.start();
                logger.info("定时发送线程重新开始执行");
                oneThreadPoolService.setSendThread(sendThread);
            }else{
                logger.info("活着无需处理");
            }

            //罗列各个线程的状态检查
            /*if(!oneThreadPoolService.iSSendAlive()){
                Thread sendThread = new Thread(new SendThread());
                sendThread.start();
                logger.info("定时发送线程重新开始执行");
                oneThreadPoolService.setSendThread(sendThread);
            }else{
                logger.info("活着无需处理");
            }*/

            logger.info("检查结束");
        }
    }

}
